﻿

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using Gift.DAL;
using Gift.Model;
using Gift.Utile;
using System.Text;

namespace WebSite.cn
{
    public partial class goodsDetail : BasePage
    {
         // Fields

        StringBuilder sbNavigater = new StringBuilder();
        readonly DALComment theComment = new DALComment();
        readonly DALModelType theModelType = new DALModelType();
        string _sModelTypeId = "";

        public string SModelTypeId
        {
            get { return _sModelTypeId; }
        }

        StringBuilder _sbModelTypeHtml = new StringBuilder();

        public StringBuilder SbModelTypeHtml
        {
            get { return _sbModelTypeHtml; }
        }
        

        public StringBuilder SbNavigater
        {
            get { return sbNavigater; }
        }
    public string clicks = "0";

    public string h3 = "0";
    public string h4 = "0";
    public string imgs = "";

    public string pic = "";
    public string price = "0";
    public string procode = "";
    public string proname = "";
    public string propinpai = "";
    public string proprice1 = "0";
    public string proprice3 = "0";
    public string proscore = "0";
    public string typename = "";

    string _sCommentCount = "";

    public string SCommentCount
    {
        get { return _sCommentCount; }
       
    }
    StringBuilder _sbStarHtml = new StringBuilder();

    public StringBuilder SbStarHtml
    {
        get { return _sbStarHtml; }

    }  

    StringBuilder _sbImagesHtml = new StringBuilder();

    public StringBuilder SbImagesHtml
    {
        get { return _sbImagesHtml; }
    }
      

    public string proku1 = string.Empty;

    public string Instruction = "";

    string _deliveryTemplateName = "";

    public string DeliveryTemplateName
    {
        get { return _deliveryTemplateName; }
    }

    readonly DALGoods theGoods = new DALGoods();
    tbGoods oGoods;
    readonly DALGoodsType theGoodsType = new DALGoodsType();
    readonly DALDeliveryTemplate theDeliveryTemplate = new DALDeliveryTemplate();
    tbDeliveryTemplate oDeliveryTemplate;
    tbGoodsType oGoodsTypeIII;
    tbGoodsType oGodsTypeII;
    tbGoodsType oGoodsType;

    // Methods
    private void BuildForm()
    {

        oGoods = theGoods.GetModel(int.Parse(Request.QueryString["gid"]));
        if (oGoods != null)
        {

            oDeliveryTemplate = theDeliveryTemplate.GetModel(oGoods.DeliveryTemplateId);
            if (oDeliveryTemplate != null)
            {
                _deliveryTemplateName = oDeliveryTemplate.TemplateName;
            }

            DataTable dtComment = theComment.GetList("proid=" + oGoods.goodsId).Tables[0];
       
            if (dtComment != null)
            {
                _sCommentCount = dtComment.Rows.Count.ToString() ;
            }
            int iCommentScore = 0;
            foreach (DataRow dr in dtComment.Rows)
            {

                iCommentScore += Convert.ToInt32(dr["score"]);

            }
            int iScore = string.IsNullOrEmpty(_sCommentCount) || _sCommentCount == "0" ? 5 : iCommentScore / int.Parse(_sCommentCount);

            for (int i = 1; i <= iScore; i++)
            {
                _sbStarHtml.Append("<img src='images/star.gif' />");
            }
            for (int j = 0; j < (5 - iScore); j++)
            {
                _sbStarHtml.Append("<img src='images/star1.gif' />");
            }


            if (oGoods.AllowAllCard == false)
            {
                this.ibtnSubmitGenerate.Visible = false;
            }
            if (!theGoods.ExistsGoodsCardType(oGoods.goodsId))
            {
                this.ibtnSubmit.Visible = false;
            }

            this.proname = oGoods.goodsName;
            this.procode = oGoods.goodsCode;

            this.proprice1 = oGoods.changePoints.ToString();

            if (oGoods.IsModelType)
            {
                DataTable tbModelType = theModelType.GetList("goodsId=" + oGoods.goodsId.ToString() + " and isnull(isDeleted,0) = 0 " ).Tables[0];
                if (tbModelType != null && tbModelType.Rows.Count > 0)
                {

                    this.price = tbModelType.Rows[0]["goodsPoints"].ToString();

                    for (int i = 0; i < tbModelType.Rows.Count; i++)
                    {
                        if (i == 0)
                        {
                            _sbModelTypeHtml.AppendFormat(" <div id=\"model{0}\" class=\"list_select\" ><a onclick=\"setModelType('#model{0}','{1}')\" >{2}</a>   </div>"
                                                        , tbModelType.Rows[i]["modelTypeId"].ToString()
                                                        , tbModelType.Rows[i]["goodsPoints"].ToString()
                                                       , tbModelType.Rows[i]["modelTypeName"].ToString()
                                                    );
                            _sModelTypeId = "#model" + tbModelType.Rows[i]["modelTypeId"].ToString();

                        }
                        else
                        {
                            _sbModelTypeHtml.AppendFormat(" <div id=\"model{0}\" class=\"list\" ><a onclick=\"setModelType('#model{0}','{1}')\" >{2}</a>   </div>"
                                                      , tbModelType.Rows[i]["modelTypeId"].ToString()
                                                      , tbModelType.Rows[i]["goodsPoints"].ToString()
                                                     , tbModelType.Rows[i]["modelTypeName"].ToString()
                                                  );

                        }
                        
                    }
                }
            }
            else
            {
                this.price = oGoods.changePoints.ToString();
            }

            this.proscore = "10000";
            this.Instruction = oGoods.instruction;
            this.proku1 = oGoods.goodsCount.ToString() ;
           
            this.clicks = oGoods.clicks.ToString();

        
         
            DataTable table3 = SQLAccess.GreatDs("select * from DB_Shoppic where proid=" + re.replace(base.Request.QueryString["gid"]) ).Tables[0];
            if (table3.Rows.Count > 0)
            {

                foreach (DataRow row in table3.Rows)
                {
                    _sbImagesHtml.AppendFormat("<li id=\"/uploadfile/{0}\" original=\"/uploadfile/{0}\"> <img alt=\"\" src=\"/uploadfile/{0}\" width=\"50\" height=\"60\"/></li>"
                                            , row["picurl"].ToString().Replace(@"\", "/")
                                            );
                }
            }
            else
            {
                _sbImagesHtml.AppendFormat("<li id=\"/uploadfile/product/{0}\" original=\"/uploadfile/product/{1}\"> <img alt=\"\" src=\"/uploadfile/product/{1}\" width=\"50\" height=\"60\"/></li>"
                                          , oGoods.bigPic.ToString().Replace(@"\", "/")
                                          , oGoods.bigPic.ToString().Replace(@"\", "/")
                                          );
            }

            oGoodsTypeIII = theGoodsType.GetModel(oGoods.typeId);
            if (oGoodsTypeIII != null)
            {
                oGodsTypeII = theGoodsType.GetModel(oGoodsTypeIII.parentId);
                if (oGodsTypeII != null)
                {
                    oGoodsType = theGoodsType.GetModel(oGodsTypeII.parentId);
                    if (oGoodsType != null)
                    {
                        this.sbNavigater.AppendFormat("<a href='/cn/hotpro.aspx?tid={0}' target='_self'>{1}</a>"
                                               , oGoodsType.typeId.ToString()
                                                ,oGoodsType.typeName
                                                  );
                    }
                    this.sbNavigater.AppendFormat("><a href='/cn/hotpro.aspx?tid={0}&subid={1}' target='_self'>{2}</a>"
                                            ,oGodsTypeII.parentId
                                             , oGodsTypeII.typeId.ToString()
                                              , oGodsTypeII.typeName
                                                );
                }
                this.sbNavigater.AppendFormat("><a href='/cn/hotpro.aspx?tid={0}&subid={1}' target='_self'>{2}</a>>{3}"
                                           , oGoodsTypeIII.parentId
                                            , oGoodsTypeIII.typeId.ToString()
                                             , oGoodsTypeIII.typeName
                                             , oGoods.goodsName
                                               );

            }

        }


    }

    public static string Displaya(string classid, string languages)
    {
        string str2 = "";
        string sql = "select top 1 parentid,classid from DB_Productclass where classid=" + classid + " and visible= 1 order by classorder";
        DataTable table = new DataTable();
        table = SQLAccess.GreatDs(sql).Tables[0];
        if (table.Rows.Count > 0)
        {
            str2 = str2 + table.Rows[0]["classid"].ToString() + "";
            if (table.Rows[0]["parentid"].ToString() != "0")
            {
                str2 = str2 + "|" + Displaya(table.Rows[0]["parentid"].ToString(), languages);
            }
        }
        return str2.ToString();
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.IsPostBack)
        {
            this.SaveLastBrowesProduct();
            string sql = "Select top 1 shopwidth1,shopheight1 From DB_Shopconfig";
            DataTable table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                this.h3 = table.Rows[0]["shopwidth1"].ToString();
                this.h4 = table.Rows[0]["shopheight1"].ToString();
            }
            if (base.Request.QueryString["gid"] != null)
            {
                this.BuildForm();
                oGoods = theGoods.GetModel(int.Parse(Request.QueryString["gid"]));
                if (oGoods != null) ;
                oGoods.clicks = oGoods.clicks + 1;
                theGoods.Update(oGoods);
                
            }
        }
    }

    string Proid
    {
        get {
            return base.Request.QueryString["gid"];
        }
     }


    void SaveLastBrowesProduct()
    {
        string strSQL = string.Empty;
        strSQL = "If NOT EXISTS(select 1 from tblLastBrowesProduct where proid = {0} and ip = '{1}') ";
        strSQL += "INSERT INTO tblLastBrowesProduct(proid,ip) values({2},'{3}')";
        SQLAccess.DoSql(string.Format(strSQL, Proid, ClientIP, Proid, ClientIP));
     }


    }
}